package com.example.note_project.ui.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.CompoundButton;
import android.widget.Switch;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.app.AppCompatDelegate;

import com.example.note_project.R;

public class NightActivity extends AppCompatActivity {

    private SharedPreferences mSharedPreferences;
    private Switch sw_night_mode;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_tools);
        initView();
    }

    private void initView() {
        sw_night_mode = (Switch) findViewById(R.id.switch1);
        mSharedPreferences = getSharedPreferences("app_night_mode", MODE_PRIVATE);

        final boolean isNightMode = mSharedPreferences.getBoolean("night_mode", false);
        if (isNightMode) {
            sw_night_mode.setChecked(true);
        } else {
            sw_night_mode.setChecked(false);
        }

        sw_night_mode.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked && !isNightMode) {
                    AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
                    mSharedPreferences.edit().putBoolean("night_mode", true).apply();
                } else if (!isChecked && isNightMode) {
                    AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
                    mSharedPreferences.edit().putBoolean("night_mode", false).apply();
                }
                startActivity(new Intent(NightActivity.this, NightActivity.class));
                finish();
            }
        });

    }
}
